/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SWANLINK_FINANCE_INNERKITS_ICCREADER_DEF_H_
#define SWANLINK_FINANCE_INNERKITS_ICCREADER_DEF_H_

#include <string>
#include <map>

namespace SwanLink {
namespace Finance {
#define MAX_NAME_LENGTH   64
#define MAX_ATTR_LENGTH   24
#define MAX_KEY_LENGTH    256
#define MAX_PATH_LENGTH   256
#define MAX_DATA_LENGTH   512

typedef enum __IOType {
    CT_ANTENNA_SWITCH = 0,  // 天线开关
    CT_ANTENNA_POWER = 1,  // 天线制式, 0:typeA, 1: typeB
    CT_BEEP = 2,
    CT_SLOT_MAIN = 3,  // 主卡座
    CT_SLOT_PSAM1 = 4,  // PSAM1卡座
    CT_SLOT_PSAM2 = 5,  // PSAM2卡座
    CT_SLOT_RF = 6,  // 非接存储卡卡座
}IOType;
typedef enum __DeviceType {
    DT_SECARD = 0,  // 社保卡
    DT_SYNCARD = 1,  // 二代证
    DT_SCAN = 2,  // 扫描
    DT_KEYBOARD = 3,  // 键盘
    DT_MAGNETIC = 4,  // 磁卡
}DeviceType;

typedef enum __VoiceMode {
    VOICE_NONE = 0,
    VOICE_PLEASE,   // 请输入密码
    VOICE_NEW,      // 请输入新密码
    VOICE_NEWAGAIN, // 请输入再次输入
    VOICE_OLD        // 请输入原来的密码
}VoiceMode;

typedef struct __DeviceInfo {
    char vendor[MAX_NAME_LENGTH];
    char deviceId[MAX_NAME_LENGTH];
    char serial[MAX_NAME_LENGTH];
    char libver[MAX_NAME_LENGTH];
    char version[MAX_NAME_LENGTH];
}DeviceInfo;
typedef struct __RfCardInfo {
    char type;
    char uid[MAX_NAME_LENGTH];
}RfCardInfo;\
//  身份证
typedef struct __IDCard {
    char photo[MAX_PATH_LENGTH];
    char name[MAX_NAME_LENGTH];
    char sex[MAX_ATTR_LENGTH];
    char nation[MAX_ATTR_LENGTH];
    char birth[MAX_ATTR_LENGTH];
    char address[MAX_PATH_LENGTH];
    char ID[MAX_ATTR_LENGTH];
    char issu_auth[MAX_ATTR_LENGTH];
    char issu_start[MAX_ATTR_LENGTH];
    char issu_end[MAX_ATTR_LENGTH];
}IDCard;

// 社保卡
struct SeCard {
    char region_code[MAX_ATTR_LENGTH];  // 行政区划代码
    char ssc_code[MAX_ATTR_LENGTH];  // 社会保障号码
    char card_num[MAX_ATTR_LENGTH];  // 卡号
    char card_identy[MAX_NAME_LENGTH];  // 卡识别码
    char name[MAX_NAME_LENGTH];  // 姓名
    char card_recover[MAX_NAME_LENGTH];  // 卡复位信息
    char version[MAX_ATTR_LENGTH];
    char issu_start[MAX_ATTR_LENGTH];  // 发卡日期
    char issu_end[MAX_ATTR_LENGTH];   // 有效期
    char term_no[MAX_ATTR_LENGTH];  // 终端机编号
    char term_device[MAX_ATTR_LENGTH];  // 终端设备号
};

struct RfCardVerify {
    int address;
    int key_mode;
    char key[MAX_KEY_LENGTH];
};

typedef struct __ResultData {
    DeviceType type;
    int  size;  // 结构体自动计算， 主要是buf的长度
    union {
        char str[MAX_DATA_LENGTH];
        uint8_t buf[MAX_DATA_LENGTH];
        RfCardInfo info;
        IDCard card;
        SeCard seCard;
    }result;
}ResultData;
typedef struct __DeviceConfig {
    DeviceType devType;
    int timeout;
    int mode;   //  默认写0
    union {
        char photo[MAX_PATH_LENGTH];
        RfCardVerify verify;
        int   findType;
    } other;
}DeviceConfig;

class SearchResult {
 public:
    virtual void OnResult(int status, const ResultData &data) = 0;
    virtual void OnError(int status, const std::string &err) = 0;
};

#define     ERR_ICC_TypeError           -10501
#define     ERR_ICC_NoExist             -10502
#define     ERR_ICC_NoPower             -10503
#define     ERR_ICC_NoResponse          -10504
#define     ERR_ConnectError            -10511
#define     ERR_UnConnected             -10512
#define     ERR_BadCommand              -10513
#define     ERR_ParameterError          -10514
#define     ERR_CheckSumError           -10515
#define     ERR_BadChipError            -10516
#define     ERR_NotFindDeivce           -10517
#define     ERR_CanNotCreateHandle      -10518   

#define DEFINE_ERR(id, err)  {id, err}

const std::map<int, const std::string> g_sErr = {
    DEFINE_ERR(ERR_ICC_TypeError,              "卡类型不符"),
    DEFINE_ERR(ERR_ICC_NoPower,                "有卡未上电"),
    DEFINE_ERR(ERR_ICC_NoExist,                "无卡或逻辑卡密码非法"),
    DEFINE_ERR(ERR_ICC_NoResponse,             "卡片无应答"),
    DEFINE_ERR(ERR_ConnectError,               "读写器连接错误"),
    DEFINE_ERR(ERR_UnConnected,                "读写器设备未连接"),
    DEFINE_ERR(ERR_BadCommand,                 "不支持该命令"),
    DEFINE_ERR(ERR_ParameterError,             "命令参数错误"),
    DEFINE_ERR(ERR_CheckSumError,              "信息检验或出错"),
    DEFINE_ERR(ERR_BadChipError,               "芯片错误"),
    DEFINE_ERR(ERR_NotFindDeivce,              "未找到设备"),
    DEFINE_ERR(ERR_CanNotCreateHandle,         "无法创建句柄")
};

} // namespace Finance
} // namespace SwanLink

#endif // SWANLINK_FINANCE_INNERKITS_ICCREADER_DEF_H_
